//
//  MenusViewController.swift
//  iModern
//
//  Created by 立雄 王 on 15/9/7.
//  Copyright (c) 2015年 com.starlooking. All rights reserved.
//

import UIKit

class MenusViewController: TableViewController {

    var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    
    let contactManage = ContactManager()
    
    var contacts:  [String:AnyObject]!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.title = "设置"
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "refresh:", name: "Refresh Menu", object: nil)

        self.tableView.registerNib(UINib(nibName: "Menu1TableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "Menu1")
        self.tableView.registerNib(UINib(nibName: "Menu2TableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "Menu2")
        self.tableView.registerNib(UINib(nibName: "Menu3TableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "Menu3")
        self.tableView.registerNib(UINib(nibName: "Menu4TableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "Menu4")
        self.loadData()
        self.initBackButton()
    }

    func loadData()
    {
        let rs = appDelegate.contactManager.loadContact()
        
        if self.verifyResutSet(rs)
        {
            if (!rs.isEmpty())
            {
                self.contacts = rs.toDictionary() as! [String:AnyObject]
            }
        }
    }
    
    @IBAction override func reloadData(sender: AnyObject!)
    {
        loadData()
        
//        super.loadContact(sender)
    }
    @IBAction func refresh(sender: AnyObject)
    {
        self.reloadData(sender)
        self.tableView.reloadData()
    }
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int
    {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return 4
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        switch indexPath.row
        {
        case 0:
            let cell    = tableView.dequeueReusableCellWithIdentifier("Menu1", forIndexPath: indexPath) as! Menu1TableViewCell
            cell.setData(contacts)
            cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
            cell.selectionStyle = UITableViewCellSelectionStyle.None
            return cell
        case 1:
            let cell    = tableView.dequeueReusableCellWithIdentifier("Menu2", forIndexPath: indexPath) as! Menu2TableViewCell
            cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
            cell.selectionStyle = UITableViewCellSelectionStyle.None
            return cell
        case 2:
            let cell    = tableView.dequeueReusableCellWithIdentifier("Menu3", forIndexPath: indexPath) as! Menu3TableViewCell
            cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
            cell.selectionStyle = UITableViewCellSelectionStyle.None
            return cell
        default:
            let cell    = tableView.dequeueReusableCellWithIdentifier("Menu4", forIndexPath: indexPath) as! Menu4TableViewCell
            cell.selectionStyle = UITableViewCellSelectionStyle.None
            return cell
        }
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    {
        switch indexPath.row
        {
        case 0: //avatar
            return 50
        case 1: //image
            return 50
        case 2:
            return 50
        default:
            return 50
        }
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        switch indexPath.row
        {
        case 0:
            let cellphone = contacts["cellphone"] as? String
            if cellphone == nil || cellphone!.isEmpty
            {
                let controller = ModifyCellphoneViewController()
                self.pushViewController(controller)
                NSNotificationCenter.defaultCenter().postNotificationName("Refresh Market", object: nil)
            }
        case 1:
            let controller = ModifyPasswordViewController()
            self.pushViewController(controller)

        case 2:
            
            UIApplication.sharedApplication().openURL(NSURL(string: "tel://4000060155")!)
        case 3:
            appDelegate.contactManager.logout()
            NSNotificationCenter.defaultCenter().postNotificationName("Logout", object: nil)
            
            self.navigationController?.popToRootViewControllerAnimated(true)
            NSNotificationCenter.defaultCenter().postNotificationName("Refresh Shop2", object: nil)
        default:
            break
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
